package com.test.test;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
/**
 * 
 * @author noahedu
 *
 */
public class ConvertUtil
{
	// TODO:值确定？？？？byte 有符号
	static final byte NOAH_FLAG_IMG = (byte) 0xB5;

	/** "【/注意】" 字节数组 --- A1BE 2FD7 A2D2 E2A1 BF **/
	static final byte[] NOTICE_ARRAY = new byte[] { (byte) 0xA1, (byte) 0xBE,
			(byte) 0x2F, (byte) 0xD7, (byte) 0xA2, (byte) 0xD2, (byte) 0xE2,
			(byte) 0xA1, (byte) 0xBF };
//	static final byte[] NOTICE_ARRAY = strToGBKByte("【/注意】");

	
	/**
	 * desp：
	 * 
	 * @param args
	 */
	public static void main(String[] args)
	{
		byte[] tArray = new byte[10];
		tArray[0] = (byte) 0x31;
		tArray[1] = (byte) 0x37;

		System.out.println(getAddFromByteBuf(tArray));

	}

	/**
	 * 获得字符串的 GBK 编码的byte数组
	 * */
	public static byte[] strToGBKByte(String str)
	{
		byte[] rArray = null;
		try
		{
			rArray = str.getBytes("GBK");
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}

		return rArray;
	}

	/**
	 * 
	 * desp：将byte 按无符号转int
	 * 
	 * @param by
	 *@return int
	 */
	public static int byteToUnsignedInt(byte by)
	{
		return Integer.parseInt(String.format("%x", by), 16);
	}

	/**
	 * 从数组读出地址
	 * */
	public static int getAddFromByteBuf(byte[] picAddBuf)
	{
		// picAddr = atoi(picAddBuf);

		int result = 0;
		int signal = 1; /* 默认为正数 */

		int i = 0;
		int temp = 0;
		while (picAddBuf[i] != 0)
		{
			temp = byteToUnsignedInt(picAddBuf[i]);
			/*
			 * '0' ==ascii 48
			 * '9' == ascii 57
			 * '+' ====43
			 * '-'===45
			 */
			if ((temp >= 48 && temp <= 57) || temp == 43 || temp == 45)
			{
				if (temp == 45)
				{
					signal = -1; /* 输入负数 */
				}
			}
			else
			{

				return 0;
			}

			i++;
		}

		i = 0;
		temp = byteToUnsignedInt(picAddBuf[i]);
		while (temp >= 48 && temp <= 57)
		{
			result = result * 10 + (temp - 48);
			i++;
			temp = byteToUnsignedInt(picAddBuf[i]);
		}
		return signal * result;
	}


	/**
	 * desp：比较srcArray从begin 到end的内容（包含begin ）是否和bArray 相等
	 * 
	 * @param srcArray
	 *@param bArray
	 *@param beginIndex
	 *@param endIndex
	 *@return
	 */
	public static boolean byteArrayCmp(byte[] srcArray, byte[] bArray,
			int beginIndex, int endIndex)
	{
		byte[] cArray = new byte[endIndex - beginIndex];

		// setting
		for (int i = 0; i < cArray.length; i++)
		{
			cArray[i] = srcArray[beginIndex + i];
		}

		return Arrays.equals(cArray, bArray);
	}
	
	

	/**
	 * desp ：将bArray 内容填充srcArray中，从beginIndex开始(包含beginIndex
	 * 
	 * @param srcArray
	 *@param bArray
	 *@param beginIndex
	 *@return
	 */
	public static boolean fillArray(byte[] srcArray, byte[] bArray, int beginIndex)
	{
         for(int i = 0;i< bArray.length; i++){
        	 srcArray[beginIndex+i] = bArray[i];
         }
         
         return false ;
	}
	/**
	 * 
	 * desp：包含begin和end
	 *@param curIndex
	 *@param begin
	 *@param end
	 *@param tmpbuf
	 *@return      i ！=0  i+1 =0
	 */
	public static int getF(int curIndex, int begin, int end, byte[] tmpbuf)
	{
		int result = -1 ;
		int i = curIndex;
//		int j = curIndex;

		if (tmpbuf[i] == 0)
		{
			if(tmpbuf[i-1]!= 0){
				result = i-1 ;
				return result ;
			}
			
			 result = getF(i/2, begin, i-1 ,tmpbuf);
			 
//			 if(result<=end && result >=begin){
//				
//			}else{
//				 result = getF(i/2,begin,end/2+1,tmpbuf);
//			}
		}
		else
		{ 
			if(tmpbuf[i+1] == 0){
				result = i ;
				return result ;
			}
			result = getF(i+ (end-i)/2   ,i+1 ,end,tmpbuf);
		}

		return result ;
	}
}
